/*jslint sloppy: true */
/*global require */
var angular = require('angular');
var LM = require('../global');
require('./register.html');
require('./register.less');

LM.controller('Register', function ($scope, Account, $location) {
	var $captcha = '';

	$scope.newUser = {};
	$scope.register = function () {
		Account.register({
			captcha: $captcha
		}, $scope.newUser, function (data) {
			$location.url('/login');
		}, function () {
			$scope.captchaInfo = true;
		});
	};

	$scope.checkExisted = function (fieldName) {
		var value = $scope.newUser[fieldName];
		if (!(value && value.length)) {
			return;
		}
		Account.isExisted({
			type: fieldName,
			value: $scope.newUser[fieldName]
		}, function (response) {
			$scope['check' + fieldName] = response.isExisted;
		});
	};
	$scope.change = function (fieldName) {
		$scope['check' + fieldName] = false;
	};

	$scope.captcha = function (newValue) {
		if (angular.isDefined(newValue)) {
			$captcha = newValue.substr(0, 4);
		}

		return $captcha;
	};

	//init
	$scope.getNow();
	$scope.queryConfig.clear().setActive();
	$scope.navConfig.setHref('#/').setType('home');
});
